#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    client = new QMQTT::Client(QHostAddress("120.76.46.85"), 1883);

    client->setClientId("clientid");
    client->setUsername("user");
    client->setPassword("password");

    ui->subBtn->setEnabled(false);
    ui->unsubBtn->setEnabled(false);
    ui->pubBtn->setEnabled(false);

    connect(client, SIGNAL(connected()), this, SLOT(doConnected()));
    connect(client, SIGNAL(disconnected()), this, SLOT(doDisonnected()));
    connect(client, SIGNAL(received(QMQTT::Message)), this, SLOT(doDataReceived(QMQTT::Message)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::doConnected()
{
    qDebug() << "connected!!";

    ui->connBtn->setText("Disconnect");

    ui->stateRadio->setChecked(true);

    ui->subBtn->setEnabled(true);
    ui->unsubBtn->setEnabled(true);
    ui->pubBtn->setEnabled(true);

    ui->eventLog->appendPlainText("Connected MQTT Server!!");
}

void MainWindow::doDisconnected()
{
    qDebug() << "disconnected!!";

    ui->connBtn->setText("Connect");

    ui->stateRadio->setChecked(false);
    ui->subBtn->setEnabled(false);
    ui->unsubBtn->setEnabled(false);
    ui->pubBtn->setEnabled(false);

    ui->eventLog->appendPlainText("Disconnected MQTT Server!!");
}

void MainWindow::doDataReceived(QMQTT::Message message)
{
    QString msg = QString(message.id())+" "+QString(message.qos())+" "+message.topic()+" "+message.payload()+"\n";
    ui->log->appendPlainText(msg);
}


void MainWindow::on_connBtn_clicked()
{
    if(!client->isConnectedToHost())
    {
        client->connectToHost();

        qDebug() << "click connect";
    }
    else
    {
        client->disconnectFromHost();

        qDebug() << "click disconnect";

        ui->connBtn->setText("Connect");

        ui->stateRadio->setChecked(false);
        ui->subBtn->setEnabled(false);
        ui->unsubBtn->setEnabled(false);
        ui->pubBtn->setEnabled(false);

        ui->eventLog->appendPlainText("Disconnected MQTT Server!!");
    }
}

void MainWindow::on_pubBtn_clicked()
{
    QString topic = ui->sendTopic->text().trimmed();
    QString payload = ui->sendPayload->text().trimmed();
    if(topic.isEmpty() || payload.isEmpty())
    {
        return;
    }
    QMQTT::Message message(136, topic, payload.toUtf8());

    client->publish(message);
}

void MainWindow::on_subBtn_clicked()
{
    QString topic = ui->subTopic->text().trimmed();
    if(topic.isEmpty())
    {
        return;
    }
    client->subscribe(topic);

    ui->eventLog->appendPlainText("subscribe topic:" + topic);
}

void MainWindow::on_unsubBtn_clicked()
{
    QString topic = ui->subTopic->text().trimmed();
    if(topic.isEmpty())
    {
        return;
    }
    client->unsubscribe(topic);

    ui->eventLog->appendPlainText("unsubscribe topic:" + topic);
}
